package com.wanna.common.lang;

import java.io.Serializable;

/**
 * @param <T>
 * @author wanna
 */
@SuppressWarnings("unchecked")
public class Result<T> implements Serializable {

    private static final int DEFAULT_SUCCESS_CODE = 0;

    private static final int DEFAULT_FAIL_CODE = -1;

    private static final String DEFAULT_SUCCESS_MESSAGE = "success";

    private static final String DEFAULT_FAIL_MESSAGE = "fail";

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    private int code;

    private boolean success;

    private String message;

    private T data;

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public Result() {
    }

    public Result(int code, boolean success, String message, T data) {
        this.code = code;
        this.success = success;
        this.message = message;
        this.data = data;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public static <T> Result success(T data) {
        return success(DEFAULT_SUCCESS_CODE, data);
    }

    public static <T> Result success(int code, T data) {
        return success(code, DEFAULT_SUCCESS_MESSAGE, data);
    }

    public static <T> Result success(String message, T data) {
        return success(DEFAULT_SUCCESS_CODE, message, data);
    }

    public static <T> Result success(int code, String message, T data) {
        return success(code, true, message, data);
    }

    public static <T> Result success(int code, boolean success, String message, T data) {
        return new Result(code, success, message, data);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public static <T> Result fail(T data) {
        return fail(DEFAULT_FAIL_CODE, data);
    }

    public static <T> Result fail(int code, T data) {
        return fail(code, DEFAULT_FAIL_MESSAGE, data);
    }

    public static <T> Result fail(String message, T data) {
        return fail(DEFAULT_FAIL_CODE, message, data);
    }

    public static <T> Result fail(int code, String message, T data) {
        return fail(code, false, message, data);
    }

    public static <T> Result fail(int code, boolean success, String message, T data) {
        return new Result(code, success, message, data);
    }

}
